import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;


/**
 * This is a class to hold various configuration options for the database.
 * Note that the database configuration file treats boolean options as true
 * if their values satisfy value.equalsIgnoreCase("true") and false otherwise,
 * i.e. parseBoolean is (setting[1].equalsIgnoreCase("true")) ? true : false
 * 
 * @author Jonathan
 *
 */
public class DBConfig {
	public static String DBCONFIG_FILENAME = "dbconfig";
	
	// User configurable settings which can be read in the dbconfig file.
	public static boolean DEBUG = true;
	public static boolean DELETE_TEMP_CPP_FILES = true;
	public static String CPP_COMPILER = "g++";
	public static String CPP_TEMP_DIR = "cppDir/";
	public static boolean CPP_COMPILE_DEBUG = false;
	public static boolean ECHO_QUERY = true;
	public static boolean TWO_PASS_ALGO = false;
	
	private DBConfig()
	{
	}

	public static void readDbConfig() throws IOException
	{
		BufferedReader in = null;
		try
		{
			in = new BufferedReader(new FileReader(DBConfig.DBCONFIG_FILENAME));
			String line;
			while((line = in.readLine()) != null)
			{
				String[] setting = line.split("=");
				if(setting.length != 2)
				{
					System.out.println("Invalid configuration setting in " + DBConfig.DBCONFIG_FILENAME + " line: " + line);
					continue;
				}
				
				switch(setting[0])
				{
					case "DEBUG":
						DBConfig.DEBUG = Boolean.parseBoolean(setting[1]);
						break;
					case "DELETE_TEMP_CPP_FILES":
						DBConfig.DELETE_TEMP_CPP_FILES = Boolean.parseBoolean(setting[1]);
						break;
					case "CPP_COMPILER":
						DBConfig.CPP_COMPILER = setting[1];
						break;
					case "CPP_COMPILE_DEBUG":
						DBConfig.CPP_COMPILE_DEBUG = Boolean.parseBoolean(setting[1]);
						break;
					case "CPP_TEMP_DIR":
						DBConfig.CPP_TEMP_DIR = setting[1];
						break;
					case "ECHO_QUERY":
						DBConfig.ECHO_QUERY = Boolean.parseBoolean(setting[1]);
						break;
					case "TWO_PASS_ALGO":
						DBConfig.TWO_PASS_ALGO = Boolean.parseBoolean(setting[1]);
						break;
					default:
						System.out.println("Read unknown configuration parameter in " + DBConfig.DBCONFIG_FILENAME + " line: " + line);
						break;
				}
			}
		}
		catch(Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			if(in != null)
			{
				in.close();
			}
		}
		
		if(DBConfig.DEBUG)
		{
			printDbConfig();
		}
	}

	private static void printDbConfig()
	{
		StringBuilder sb = new StringBuilder();
		sb.append("DEBUG=");
		sb.append(DBConfig.DEBUG);
		sb.append("\nDELETE_TEMP_CPP_FILES=");
		sb.append(DBConfig.DELETE_TEMP_CPP_FILES);
		sb.append("\nCPP_COMPILER=");
		sb.append(DBConfig.CPP_COMPILER);
		sb.append("\nCPP_COMPILE_DEBUG=");
		sb.append(DBConfig.CPP_COMPILE_DEBUG);
		sb.append("\nCPP_TEMP_DIR=");
		sb.append(DBConfig.CPP_TEMP_DIR);
		sb.append("\nECHO_QUERY=");
		sb.append(DBConfig.ECHO_QUERY);
		sb.append("\nTWO_PASS_ALGO=");
		sb.append(DBConfig.TWO_PASS_ALGO);
		System.out.println("Current DB Configuration:\n" + sb);
	}
	
	
}
